package com.syb.util;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class AuthorityInterceptor  extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String url = ServletActionContext.getRequest().getRequestURL().toString();  
		 HttpServletResponse response=ServletActionContext.getResponse();  
		 if (url.indexOf("login")!=-1 || url.indexOf("logout")!=-1){  
	            return invocation.invoke();  
	        } else{  
	            //验证Session是否过期  
	            if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){  
	                //session过期,转向session过期提示页,最终跳转至登录页面  
	                return "timeout";  
	            }  
	            else{  
	                ServletActionContext.getRequest().getSession().getAttribute("userSession");  
	                //验证是否已经登录  
	                if (ServletActionContext.getRequest().getSession().getAttribute("userSession")==null){  
	                    //尚未登录,跳转至登录页面  
	                    return "login";  
	                }else{                      
	                    return invocation.invoke();  
	                                  
	                }                  
	            }   
	        }
		/*System.out.print(invocation.getInvocationContext().getSession().get("userSession"));
		System.out.print("进入拦截器========================");
		return invocation.invoke();*/
	}

}

